package com.lunz.machine.fragment;

import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.lunz.machine.R;
import com.lunz.machine.adapter.CheckCarPageAdapter;
import com.lunz.machine.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;

/**
 * @ClassName HomeFragment
 * @Description 首页Fragment
 * @Author ：WangZhuang
 * @Date : 2020/10/13 13:24
 * @Version :
 */
public class HomeFragment extends BaseFragment {
    @InjectView(R.id.tabLayout)
    TabLayout tabLayout;
    @InjectView(R.id.vp)
    ViewPager vp;
    private String TAG = HomeFragment.class.getSimpleName();
    private String[] tabs = new String[]{"未开始", "作业中", "已完成"};
    private List<Fragment> fragmentList = new ArrayList<>();
    private CheckCarPageAdapter mAdapter;

    @Override
    public void onCreateView(ViewGroup container) {
        setView(R.layout.fragment_home, container, false, true);
        initTab();
        initPage();
    }

    private void initTab() {
        for (int i = 0; i < 3; i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            tab.setText(tabs[i]);
            tabLayout.addTab(tab);
        }
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                TextView textView = new TextView(getActivity());
                textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                textView.setTextColor(getResources().getColor(R.color.color_E6000000));
                textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                textView.setText(tab.getText());
                textView.setGravity(Gravity.CENTER);
                tab.setCustomView(textView);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void initPage() {
        fragmentList.add(new HomeNoStartFragment());
        fragmentList.add(new HomeWorkingFragment());
        fragmentList.add(new HomeCompletedFragment());
        mAdapter = new CheckCarPageAdapter(getChildFragmentManager(), mActivity, fragmentList, tabs);
        vp.setAdapter(mAdapter);
//        vp.setOffscreenPageLimit(3);
        tabLayout.setupWithViewPager(vp);
    }
}
